<?php
namespace Admin\Controller;
use Admin\Controller\CommonController;
use Common\Common\Dateformat;
class UserController extends CommonController {

    public function indexAction(){
        $model = M();
        $sql = "select t1.id,t1.username,t1.mail,t1.register_time,t1.logout_time,t1.online_time,t2.group_name from user t1 inner join grouplist t2 on t1.gid =t2.id";
        $useInfo = $model->query($sql);
        $date = new Dateformat;
        foreach ($useInfo as &$v) {
            $v['logout_time'] = $date -> dateformat($v['logout_time']);
            $v['online_time'] = $date -> format($v['online_time']);
        }
        $this->assign('useInfo',$useInfo);
        $this->display('index');
    }

    /**
     * 添加用户
     */
    public function addAction(){
        $model =M();
        $sql = "select group_name from grouplist";
        $groupInfo = $model->query($sql);
        $this->assign('groupInfo',$groupInfo);

        if(IS_POST){
            $username = I('post.username');
            $password = md5(I('post.password'));
            $mail = I('post.email','','validate_email');
            $group_name = I('post.group_name');

            // 判断邮箱，用户名，密码
            if (empty($mail)) $this->error('邮箱格式不正确','',2);
            if(empty($username)) $this->error('请填写用户名','',2);
            if(empty($password)) $this->error('密码不能留空','',2);

            // 判断用户名是否存在
            $isExist = $model->query("SELECT * FROM user where username='{$username}'");
            if (!empty($isExist)) $this->error('用户已存在','',2);

            if($group_name=="管理员"){
               $gid = 1;
            }elseif($group_name=="会员"){
                $gid = 2;
            }else{
                $gid = 3;
            }

            $register_time = time();
            $sql = "insert into user (username,password,mail,register_time,gid) VALUES('{$username}','{$password}','{$mail}','{$register_time}','{$gid}')";

            if($model->execute($sql)){
                $this->success('注册成功',U('Admin/User/index'),1);
            }
        }else{
            $this->display('add');
        }
    }

    /**
     * 编辑用户
     */
    public function editAction(){
        $id = I('get.id');
        $model = M();
        $sql = "select * from user where id ='{$id}'";
        $userInfo = $model->query($sql);
        $userInfo = $userInfo[0];
        $this->assign('userInfo',$userInfo);

        $sql = "select * from jobs";
        $jobsInfo = $model->query($sql);
        $this->assign('jobsInfo',$jobsInfo);

        $sql = "select * from grouplist";
        $groupInfo = $model->query($sql);
        $this->assign('groupInfo',$groupInfo);

        $this->display('edit');
    }

    /**
     * 更新用户资料
     */
    public function updateAction(){
        $postData = I('post.');
        $model = M();
        if($postData['group_name'] =='管理员') {
            $gid = 1;
        }elseif($postData['group_name'] =='会员'){
            $gid = 2;
        }else{
            $gid = 3;
        }

        $sql= "update user set gid='{$gid}',job='{$postData['job']}',sex='{$postData['sex']}',username='{$postData['user_name']}',province='{$postData['province']}',introduce='{$postData['signature']}',qq='{$postData['qq']}',phone='{$postData['mobile']}',mail='{$postData['email']}',site='{$postData['homepage']}',city='{$postData['city']}' where id='{$postData['id']}'";
        $model->execute($sql);
        if(!empty($postData['password'])){
            $newPassword = md5($postData['password']);
            $sql= "update user set password='{$newPassword}' where id='{$postData['id']}' ";
            $model->execute($sql);
        }
        $this->success('修改成功','');

    }

     /**
      * 封禁用户
      */
     public function closeAction(){
         $id = I('get.id');
         $model = M();
         $sql = "update user set gid=3 where id='{$id}'";
         if($model->execute($sql)){
             $this->success('封禁用户成功',U('Admin/User/index'),1);
         }

     }

}